<template>
  <div>
    <el-input v-model="changeValue" v-bind="{ ...item }" />
  </div>
</template>

<script>
import mixin from '../mixin'
import { isLabelTag } from '../../GenerateForm/FormItemLabel'
// 动态使用的方法，需要在这里声明 不要移除了！！！
let {
  // 数学函数
  SUM,
  AVERAGE,
  ABS,
  COUNT,
  COUNTIF,
  CEILING,
  FLOOR,
  MAX,
  MIN,
  INT,
  LOG,
  MOD,
  RAND,
  POWER,
  PRODUCT,
  LARGE,
  ROUND,
  SMALL,
  SQRT,
  SUMPRODUCT,
  // 文字函数
  CONCATENATE,
  CHAR,
  EXACT,
  LEFT,
  LEN,
  LOWER,
  MID,
  REPLACE,
  REPT,
  RIGHT,
  SEARCH,
  SPLIT,
  TRIM,
  UPPER,
  VALUE,
  // 日期函数
  DATE,
  DATEDIF,
  DAY,
  DAYS,
  DAYS360,
  HOUR,
  ISOWEEKNUM,
  MINUTE,
  MONTH,
  NETWORKDAYS,
  NOW,
  SECOND,
  TIME,
  TODAY,
  WEEKNUM,
  WORKDAY,
  YEAR,
  //  逻辑函数
  AND,
  FALSE,
  IF,
  IFS,
  NOT,
  OR,
  TRUE,
  XOR
} = require('../../../lib/formulajs/index.js')

export default {
  name: 'TFunCalc',
  mixins: [mixin],
  props: ['models'],
  watch: {
    models: {
      handler(val) {
        let calcFormula = this.item.calcFormula
        if (!calcFormula) {
          return
        }
        let flag = false
        Object.keys(val).forEach((key) => {
          if (calcFormula.indexOf(`#{${key}}`) !== -1) {
            flag = true
            // 处理数据格式
            calcFormula = calcFormula.replaceAll(`#{${key}}`, this.handleValue(key, val[key]))
          }
        })
        if (!flag) return
        console.log(calcFormula)
        let evalVal = eval(calcFormula)
        if (evalVal != null) {
          this.changeValue = evalVal
        }
      },
      deep: true
    }
  },
  data() {
    return {}
  },
  methods: {
    /**
     * 处理数据格式
     * @param key
     * @param val
     */
    handleValue(key, val) {
      // 判断文字是否是已xx开头
      if (key.startsWith('number')) {
        return val
      } else if (isLabelTag(key)) {
        return `'${this.models[`${key}label`]}'`
      } else {
        return `'${val}'`
      }
    }
  }
}
</script>

<style lang="scss" scoped></style>
